#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    map<vector<int>, vector<uint64_t>> m;
    for (uint64_t i = 0; ; ++i) {
        vector<int> digits;
        uint64_t p = i * i * i;
        uint64_t t = p;
        while (p) {
            digits.push_back(p % 10);
            p /= 10;
        }
        sort(digits.begin(), digits.end());
        auto it = m.find(digits);
        if (it != m.end()) {
            m[digits].push_back(t);
            if (m[digits].size() == 5) {
                vector<uint64_t>& v = m[digits];
                sort(v.begin(), v.end());
                cout << v[0] << endl;
                return 0;
            }
        } else {
            m[digits] = {t};
        }
    }
}